package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.dto.bpa.DeductionRecordQueryDto;
import top.infopub.enums.bpa.DeductionModeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.jsonVo.DeductionRecordDetailJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.DeductionRecordService;
import top.infopub.mgr.bpa.vo.DeductionRecordOtherVo;
import top.infopub.mgr.bpa.vo.DeductionRecordVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.model.bpa.DeductionRecord;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: xieyong
 * @Date: 2021/6/20 16:14
 * @Since 1.0
 **/
@Controller
@RequestMapping("/deductionRecord")
public class DeductionRecordController {
    @Autowired
    private DeductionRecordService deductionRecordService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        List<Map<String, String>> deductionModes = DeductionModeEnum.asList();
        request.setAttribute("deductionModes",deductionModes);
        return "bpa/lay-DeductionRecord-list";
    }

    /**
     * @param request:
     * @param id:
     * @param proCode:
     * @Description: 进入详情页
     * @Author: xieyong
     * @Date: 2021/6/20 10:49
     * @return: java.lang.String
     **/
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        request.setAttribute("id", id);
        DeductionRecord bizPlan = deductionRecordService.selectByPrimaryKey(id);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
        request.setAttribute("finFlag", "0");//指定财务人员进入审批页面必须填写财务信息
        if (bizPlan != null) {
            if (org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bizPlan.getProcessStatus())
                    || org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), bizPlan.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(bizPlan.getProcessId()) && bizPlan.getProcessId().length() > 10) {
                request.setAttribute("processId", bizPlan.getProcessId());
                String auditTabFlag = bpaProcessManageService.checkShowAuditTab(bizPlan.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bizPlan.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        request.setAttribute("proCode", proCode);
        return "bpa/lay-DeductionRecord-detail";
    }

    /**
     * @param deductionRecordQueryDto: 分页列表查询参数
     * @param paramPage:               分页参数
     * @Description: 商务成本支付列表查询
     * @Author: xieyong
     * @Date: 2021/6/20 9:27
     * @return: top.infopub.mgr.common.vo.RspVo<top.infopub.mgr.bpa.vo.DeductionRecordVo>
     **/
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<DeductionRecordVo> queryDataByCondition(DeductionRecordQueryDto deductionRecordQueryDto, ParamPage paramPage) {
        Page<DeductionRecordVo> page = new Page<DeductionRecordVo>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = deductionRecordService.selectByDomainWhere(page, deductionRecordQueryDto);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * @param id: 主键
     * @Description: 详情页获取商务成本支付详情
     * @Author: xieyong
     * @Date: 2021/6/20 10:19
     * @return: top.infopub.mgr.common.vo.RspVo
     **/
    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        DeductionRecordDetailJsonVo detailJson = deductionRecordService.queryDetailById(id);
        return RspVo.getSuccessResponse(detailJson);
    }

    //根据procode获取项目的基本信息
    @RequestMapping("/queryProjDetail")
    @ResponseBody
    public RspVo queryProjDetail(String proCode) {
        DeductionRecordDetailJsonVo deduRecJson = new DeductionRecordDetailJsonVo();
        //项目的基础信息
        BpaPublicProInfoDto dto = projectInfoService.queryBpaProInfoByProCode(proCode);
        DeductionRecordOtherVo info = new DeductionRecordOtherVo();
        BeanUtils.copyProperties(dto, info);
        deduRecJson.setInfo(info);
        return RspVo.getSuccessResponse(deduRecJson);
    }

    /**
     * @param bodyStr:    前端参数
     * @param submitType: 类型
     * @Description:
     * @Author: xieyong
     * @Date: 2021/6/20 10:43
     * @return: top.infopub.mgr.common.vo.RspVo
     **/
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, Integer submitType) {
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        DeductionRecordDetailJsonVo detailJson = JSON.parseObject(str, DeductionRecordDetailJsonVo.class);
        RspVo rspVo = deductionRecordService.save(detailJson, submitType);
        return rspVo;
    }
}
